package xyz.xiaomeiqiu.study;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
import xyz.xiaomeiqiu.study.redis.PubOrder;
import xyz.xiaomeiqiu.study.redis.SubOrder;

import java.util.Collections;
import java.util.Random;

public class App {
    private static final Logger logger = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) throws InterruptedException {
        JedisPool jedisPool = new JedisPool();

        consumeOrders(jedisPool);
        publishOrders(jedisPool);

    }

    private static Random random = new Random();

    public static void publishOrders(JedisPool jedisPool) throws InterruptedException {
        Thread t = new Thread(() -> {
            int count = 0;
            PubOrder pubOrder = new PubOrder(jedisPool);
            while (count<10){
                pubOrder.publish("订单"+System.currentTimeMillis()+"_"+random.nextInt(1000)+"号");
                try {
                    Thread.sleep(1000+random.nextInt(3000));
                    count++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            pubOrder.publish("end");
        });
        t.start();
    }

    public static void consumeOrders(JedisPool jedisPool) throws InterruptedException {
        SubOrder subOrder = new SubOrder(jedisPool);
        subOrder.start();
    }


}